<?php
class UserBank extends CS_Controller
{
    public function _init()
    {
        $this->load->library('pagination');
        $this->load->model('user_bank_model', 'user_bank');
        $this->load->model('pay_method_model', 'pay_method');
        $this->load->model('user_type_model', 'user_type');
    }

    public function bankList()
    {
        $data['userBank'] = $this->user_bank->findByUid($this->uid);
        $this->load->view('userbank/bankList', $data);
    }

    public function details($id)
    {
        $userBank = $this->user_bank->findBankInfo($id, $this->uid);
        if ($userBank->num_rows() == 0) {
            $this->alertJumpPre('银行卡不存在');
        }
        $data['bankInfo'] = $userBank->row();
        $this->load->view('userbank/details', $data);
    }

    //添加卡号
    public function bankAdd()
    {
        $data['bankList'] = $this->pay_method->findShortcut(2)->result();
        $this->load->view('userbank/bankAdd', $data);
    }
    
    //添加卡号
    public function addPost()
    {
        $postData = $this->_validate();
        $postData['uid'] = $this->uid;

        $this->db->trans_start();
        if ($postData['is_default'] == 1) {
            $this->user_bank->setNoDefault($this->uid);
        } else {
            $count = $this->user_bank->total($this->uid);
            if ( $count == 0 ) {
                $postData['is_default'] = 1;
            }
        }
        $data = array(
            'uid'           => $postData['uid'],
            'bank_id'       => $postData['bank_id'],
            'bank_name'     => $postData['bank_name'],
            'user_type'     => 1,
            'bank_card'     => $postData['bank_card'],
            'pin'           => $postData['pin'],
            'cardholder'    => $postData['cardholder'],
            'mobile_phone'  => $postData['mobile_phone'],
            'bank_type'     => 1,
            'is_default'    => empty($postData['is_default']) ? 0 : $postData['is_default'],
            'created_at'    => date('Y-m-d H:i:s')
        );
        $result = $this->user_bank->insert($data);
        $this->db->trans_complete();
        if ($this->db->trans_status() === FALSE) {
            $this->ajaxJson(false, '银行卡绑定失败');
        } else {
            $this->ajaxJson(true, site_url('userBank/bankList'));
        }
    }

    /**
     * 设为默认
     */
    public function setDefault($id)
    {
        $userBank = $this->user_bank->findById($id, $this->uid);
        if ($userBank->num_rows() == 0) {
            $this->alertJumpPre('银行卡不存在');
        }

        $this->db->trans_start();
        $this->user_bank->setNoDefault($this->uid);
        $this->user_bank->updateInfo($id, $this->uid, array('is_default' => 1));
        $this->db->trans_complete();

        if ($this->db->trans_status() === FALSE){
            $this->alertJumpPre('操作失败');
        } else {
            redirect($_SERVER['HTTP_REFERER']);
        }
    }
    
    //删除卡号
    public function delete($id)
    {
        $userBank = $this->user_bank->findById($id, $this->uid);
        if ($userBank->num_rows() == 0) {
            $this->alertJumpPre('银行卡不存在');
        }
        $result = $this->user_bank->delete($id, $this->uid);
        if ($result) {
            redirect('userBank/bankList');
        } else {
            $this->alertJumpPre('银行卡删除失败');
        }
    }

    /**
     * 验证
     */
    private function _validate()
    {
        $postData = $this->input->post();
        if (!$postData['bank_id']) {
            $this->ajaxJson(false, '请选择银行卡类型');
        }

        if ($postData['user_type'] == 1) {
            $payBank = $this->pay_method->findById($postData['bank_id']);
            if ($payBank->num_rows() == 0) {
                $this->ajaxJson(false, '银行卡类型不正确');
            }

            if (!validateBankCard($postData['bank_card'])) {
                $this->ajaxJson(false, '请填写正确的银行卡号');
            }

            if (!validateChineseName($postData['cardholder'])) {
                $this->ajaxJson(false, '请填写正确的持卡人姓名');
            }

            if (!validateSfz($postData['pin'])) {
                $this->ajaxJson(false, '请输入正确的身份证号码');
            }
            $postData['bank_name']  = $payBank->row()->bank_name;
        } elseif ($postData['user_type'] == 2) {
            if (empty($postData['company']) || strlen($postData['company']) < 4) {
                $this->ajaxJson(false, '请输入正确的公司名称');
            }

            if (empty($postData['bank_name']) || strlen($postData['bank_name']) < 4) {
                $this->ajaxJson(false, '请输入正确的银行名称');
            }

            if (empty($postData['company_bank']) || strlen($postData['company_bank']) < 6) {
                $this->ajaxJson(false, '请输入正确的对公帐号');
            }
            $postData['cardholder'] = $postData['company'];
            $postData['bank_card']  = $postData['company_bank'];
        } else {
            $this->ajaxJson(false, '请选择是类别，个人还是公司');
        }

        if (!validateMobilePhone($postData['mobile_phone'])) {
            $this->ajaxJson(false, '请输入正确的手机号码');
        }

        if (!strlen($postData['captcha']))  {
            $this->ajaxJson(false, '请填写效验码');
        }

        if (strlen($postData['captcha']) != 6) {
            $this->ajaxJson(false, '短信效验码错误');
        }

        $endTime = date('Y-m-d H:i:s');
        $result = $this->user_code->findOne(['mobile' => $postData['mobile_phone'], 'code' => $postData['captcha']], 'id, end_time');//, 'end_time' => $endTime
        if ($result->num_rows() <= 0) {
            $this->ajaxJson(false, '手机验证码错误');
        }
        if ($result->row(0)->end_time < $endTime) {
            $this->ajaxJson(false, '手机验证码超过有效期，请重新获取~');
        }

        return $postData;
    }
}